package com.wcbdota.ui;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.wcbdota.R;
import com.wcbdota.base.BaseMessage;
import com.wcbdota.base.BaseUi;
import com.wcbdota.base.C;
import com.wcbdota.base.Messages;
import com.wcbdota.model.Version;
import com.wcbdota.ui.UiItemContent.ButtonListener;
import com.wcbdota.util.AppUtil;
import com.wcbdota.util.AsyncAppLoader;
import com.wcbdota.util.AsyncAppLoader.AppLoaderCallback;
import com.wcbdota.util.SDUtil;

public class UiSetting extends BaseUi {

	private static String TAG = UiSetting.class.getSimpleName();
	private Context mContext = UiSetting.this.getContext();
	private ArrayList<Version> versionObjList = new ArrayList<Version>();
	
	private TextView pt_tv_titlename;
	
	private RadioButton rbtn_on,rbtn_off;
	private RadioGroup rdgroup;
	private SharedPreferences sp;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setting_main);
		this.doTaskAsync(C.task.versionList, C.api.versionList);
		init();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	protected void onResume() {
		super.onResume();
		try {
			versionObjList = (ArrayList<Version>) Messages.msg.msg_UiSetting_VersionList.getResultList("Version");
		} catch (Exception e) {
			Log.w(TAG, "cache Msg error");
		}
	}

	// //////////////////////////////////////////////////////////////////////////////////////////////
	// async task callback methods
	
	@SuppressWarnings("unchecked")
	@Override
	public void onTaskComplete(int taskId, BaseMessage message) {
		super.onTaskComplete(taskId, message);

		switch (taskId) {
		case C.task.versionList:			
			try {
				Messages.msg.msg_UiSetting_VersionList = message;
				versionObjList = (ArrayList<Version>) Messages.msg.msg_UiSetting_VersionList.getResultList("Version");
			} catch (Exception e) {
				toast(e.getMessage());
			}
			break;
		}
	}

	@Override
	public void onNetworkError(int taskId) {
		super.onNetworkError(taskId);
	}

	// //////////////////////////////////////////////////////////////////////////////////////////////
	// other methods

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		return super.onKeyDown(keyCode, event);
	}

	public void init() {
		findViewById(R.id.sm_bt_clearcache).setOnClickListener(mOnClickListener);
		findViewById(R.id.sm_bt_checkupdate).setOnClickListener(mOnClickListener);
		findViewById(R.id.pt_bt_back).setOnClickListener(mOnClickListener);
		findViewById(R.id.pt_bt_reload).setOnClickListener(mOnClickListener);
		pt_tv_titlename = (TextView) findViewById(R.id.pt_tv_titlename);
		pt_tv_titlename.setText("设置");
		rbtn_on =(RadioButton)findViewById(R.id.rbtn_on);
		rbtn_off=(RadioButton)findViewById(R.id.rbtn_off);
		rdgroup=(RadioGroup)findViewById(R.id.rdgroup);
		
		rdgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){
		
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				// TODO Auto-generated method stub
				if(checkedId==R.id.rbtn_on){
					
					sp =getSharedPreferences("Screem",0);
					sp.edit().putBoolean("isOn", true).commit();
				}else{
					sp =getSharedPreferences("Screem",0);
					sp.edit().putBoolean("isOn", false).commit();
				}
				
			}});
			}
	
	private void checkVersion(){
		if(AppUtil.getVersionCode(mContext) != Integer.valueOf( versionObjList.get(0).getVersionCode()) ){
			showVersionUpdate();
		}
	}
	
	private void showVersionUpdate() {
		String message = "当前版本：" + AppUtil.getVesionName(mContext) + "更新版本：" + versionObjList.get(0).getVersionName();
	    new AlertDialog.Builder(mContext)
	    	.setTitle("更新")
	    	.setMessage(message)
	    	.setPositiveButton("下载更新", PositiveButtonOnClickListener)
	    	.setNegativeButton("暂不更新", NegativeButtonOnClickListener)
	    	.show();
	}
	
	private void showClearCache(){
		new AlertDialog.Builder(mContext)
		.setTitle("清空缓存")
		.setMessage("确认要清空缓存吗？")
		.setPositiveButton("确定", SureOnClickListener)
    	.setNegativeButton("取消", null)
    	.show();
	}
	
	DialogInterface.OnClickListener SureOnClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			String path ="/mnt"+C.dir.images+"/";
			SDUtil.removeCache(path);
		}
	};

	// //////////////////////////////////////////////////////////////////////////////////////////////
	// Listener
	OnClickListener mOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.pt_bt_back:
				finish();
				break;
			case R.id.pt_bt_reload:
				toast("已是最新数据 :) ");
				break;
			case R.id.sm_bt_clearcache:
				showClearCache();
				break;
			case R.id.sm_bt_checkupdate:
				checkVersion();
				break;
			}
		}
	};
	
	DialogInterface.OnClickListener PositiveButtonOnClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			showLoadBar();
            dialog.dismiss();
            new AsyncAppLoader().loadApp( new AppLoaderCallback() {
				@Override
				public void AppLoaded(int result) {
					Log.i("load", "文件下载完成");
					hideLoadBar();
					//运行更新程序
					AppUtil.installUpdate(mContext);
				}
			});
		}
	};
	
	DialogInterface.OnClickListener NegativeButtonOnClickListener = new DialogInterface.OnClickListener() {
		@Override
		public void onClick(DialogInterface dialog, int which) {
			dialog.dismiss();
		}
	};
}
